#!/usr/bin/env python

import os
import logging
import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from volunteers import Volunteer

##
## TODO: Mock all pages; then add images
## TODO: Google Apps, Email, Calendar, etc. https://www.google.com/a/cpanel/africanorphanage.org/Dashboard
## TODO: Blog (incl. on site); http://help.blogger.com/bin/answer.py?answer=55373
## TODO: Picassa folders?
## TODO: Profile for all members; login
## TODO: Home location and map for all members
## TODO: Travel guide; postcards
##

class MainHandler(webapp.RequestHandler):

  Pages = [
    { 'path' : 'home',        'title' : 'Home' },
    { 'path' : 'about',       'title' : 'About us' },
    { 'path' : 'programmes',  'title' : 'Programmes' },
    { 'path' : 'volunteer',   'title' : 'Volunteer' },
    { 'path' : 'support',     'title' : 'Support us' },
    { 'path' : 'sponsor',     'title' : 'Sponsor a child' },
    { 'path' : 'map',         'title' : 'Map' },
  ]

  MemberPages = [
    { 'path' : 'members',     'title' : 'Members' },
  ]

  ValidPages = [ 'home', 'about', 'volunteer', 'programmes', 'support', 'sponsor', 'map', 'members' ]

  ShowVolunteers = [ 'map', 'members' ]

  def get(self):
    user = users.get_current_user()
    admin = users.is_current_user_admin()

    member_pages = None
    if user:
      member_pages = MainHandler.MemberPages

    parts = self.request.path.split('/')
    page = parts[len(parts)-1]

    if not page:
      page = 'home'  ## 'welcome'
    elif not page in MainHandler.ValidPages:
      self.redirect('/home')
      return

    template_file = 'templates/' + page + '.html'

    volunteers = None
    if page in MainHandler.ShowVolunteers:
      volunteers = Volunteer.all().order('first_name').fetch(100)

    values = {
      'logout_url'    : users.create_logout_url('/home'),
      'login_url'     : users.create_login_url('/members'),
      'user'          : user,
      'admin'         : admin,
      'pages'         : MainHandler.Pages,
      'member_pages'  : member_pages,
      'current'       : page,
      'volunteers'    : volunteers,
      'email_contact' : 'info@watotowabaraka.org'
    }

    path = os.path.join(os.path.dirname(__file__), template_file)
    self.response.out.write(template.render(path, values))


def main():
  application = webapp.WSGIApplication([
      ('.*', MainHandler),
    ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
